package quantri.login;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import utils.StringUtils;
import utils.UtilsOnScopes;
import dbutil.ConnectDB;

/******************************************
 * Filename			: User.java
 * Decription		: Bean của user đăng nhập [login func]
 * Created date		: May, 2008
 * Author			: Honglk
 * Modify 1			: Phonglt (Jun 2, 2008)
 * Note of modify 1	: delete comment, modify method
 ******************************************/
public class User {
	public String userName;
	private String password;
	public  String login_logout = "Đăng nhập";
	private String chancePass;
	private String isEnable;
	private String pathMenu_Main ="/templates/default/temp.xhtml" ;
	public User(String userName, String password, List<String[]> arr) {
		this.userName = userName;
		this.password = password;
	}

	public User() {
	}

	
	public String getPathMenu_Main() {
		return pathMenu_Main;
	}

	public void setPathMenu_Main(String pathMenu_Main) {
		this.pathMenu_Main = pathMenu_Main;
	}

	/****************************************************
	 * Method name		: getUserName [GETTER]
	 * Return type		: User
	 * Decription		: 
	 * Created date		: Jun 2, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public String getUserName() {
		System.out.println("get Username: " + this.userName);
		return userName;
	}

	/****************************************************
	 * Method name		: setUserName [SETTER]
	 * Return type		: void
	 * Decription		: 
	 * Created date		: Jun 2, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public void setUserName(String userName) {
		System.out.println("set username: " + userName);
		this.userName = userName;
	}

	/****************************************************
	 * Method name		: getPassword [GETTER]
	 * Return type		: User
	 * Decription		: 
	 * Created date		: Jun 2, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public String getPassword() {
		return password;
	}

	/****************************************************
	 * Method name		: setPassword [SETTER]
	 * Return type		: void
	 * Decription		: 
	 * Created date		: Jun 2, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public void setPassword(String password) {
		this.password = password;
	}

	/****************************************************
	 * Method name		: getLogin_logout [GETTER]
	 * Return type		: User
	 * Decription		: 
	 * Created date		: Jun 2, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public String getLogin_logout() {
		return login_logout;
	}

	/****************************************************
	 * Method name		: setLogin_logout [SETTER]
	 * Return type		: void
	 * Decription		: 
	 * Created date		: Jun 2, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public void setLogin_logout(String login_logout) {
		this.login_logout = login_logout;
	}
	
	

	public String getChancePass() {
		return chancePass;
	}

	public void setChancePass(String chance) {
		this.chancePass = chance;
	}
	

	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	/**************************************
	 * Method name		: CheckLogin
	 * Return type		: String
	 * Decription		: kiểm tra và thực hiện thao tác đăng nhập
	 * Created date		: May 2, 2008
	 * Author			: Honglk
	 * Modify 1			: Phonglt (Jun 2, 2008)
	 * Modify 2 		: Honglk (Jun 14, 2008)
	 * Note of modify 1	: encrypt -> check
	 **************************************/
	public String CheckLogin() {
		System.out.println("Check login: " + this.userName + " - " + this.password);
		String result = "";
		//TODO nên sửa lại câu SQL thành từng đoạn cho an toàn
		String SQL = "SELECT A.pass,id FROM thanhvien A WHERE A.name='"
				+ this.userName + "'";
		String encrypt = "";
		try {
			ArrayList<HashMap<String, Object>> arr = ConnectDB.getDataTypeList(SQL);
			if(arr.size() > 0){
			HashMap<String, Object> hash = (HashMap<String, Object>) arr.get(0);
			String pass = hash.get("pass").toString();
			String idThanhVien = hash.get("id").toString();
			encrypt = StringUtils.encryptString(this.password);
			if (encrypt.equals(pass)) {
				HttpSession ses = UtilsOnScopes.getSession();
				ComponentNavigator cn = (ComponentNavigator) ses
						.getAttribute("componentNavigator");
				if (cn == null) {
					System.out.println("cn hok co");
					cn = new ComponentNavigator();
				}
				cn.updateDataSrc(idThanhVien);
				ses.setAttribute("componentNavigator", cn);
				 this.setLogin_logout("Thoát");
				 this.setChancePass("Đổi mật khẩu");
				 this.setIsEnable("false");
				 this.setPathMenu_Main("/templates/default/main_function.xhtml");
//				 Schedule.schedule();
				 System.out.println("success");
				result = "success";
			} else {
				result = "fail";
			}
			}
		} catch (Exception e) {
			this.setLogin_logout("Đăng nhập");
			this.setIsEnable("true");
			this.setChancePass("");
			e.printStackTrace();
		}
		return result;
	}

	/**************************************
	 * Method name		: logout
	 * Return type		: String
	 * Decription		:
	 * Created date		: May, 2008
	 * Author			: Honglk
	 * Modify 1			: Phonglt (Jun 4, 2008)
	 * Modify 2 		: Honglk (Jun 14, 2008)
	 **************************************/
	public String logout() {
		//TODO xóa cả user
		HttpSession session = (HttpSession)UtilsOnScopes.getSession();
		try{
		session.removeAttribute("componentNavigator");
		session.removeAttribute("user");
		this.setLogin_logout("Đăng nhập");
		this.setIsEnable("true");
		this.setChancePass("");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return "logout";
	}
	
	/**************************************
	 * Method name		: setLogin_Logout
	 * Return type		: void
	 * Decription		:
	 * Created date		: May, 2008
	 * Author			: Honglk
	 **************************************/
	public void setLogin_Logout() {
		this.setLogin_logout("Thoát");
	}
	
	
}